package com.fengzai.demo.controller;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import com.fengzai.demo.annotation.MyController;
import com.fengzai.demo.annotation.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @PACKAGE_NAME: com.fengzai.demo.controller
 * @author: rhf
 * @ProjectName: netty
 * @DATE: 2021/11/3
 **/
@MyController
@RequestMapping("/test2/")
public class TestController2 {
    @GetMapping("1")
    public String test() {
        return "1";
    }

    @Autowired
    private ApplicationContext applicationContext;
    @GetMapping("2")
    public String test2() {
        String[] beanDefinitionNames = applicationContext.getBeanNamesForAnnotation(MyController.class);
        for (String name:beanDefinitionNames)
        {
            System.out.println(name);
        }
        System.out.println("--------");

        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(MyService.class);
        for (String name:beansWithAnnotation.keySet())
        {
            Object o = beansWithAnnotation.get(name);
            Class<?>[] interfaces = o.getClass().getInterfaces();
            for (Class tc: interfaces)
            {
                System.out.println(name+ "--"+ tc.getTypeName());
                Class<?>[] interfaces1 = tc.getClass().getInterfaces();
                for (Class tc1: interfaces1)
                {
                    System.out.println(name+ "-----"+ tc1.getTypeName());

                }
            }
        }
        return "2";
    }

    public static void main(String[] args) {
        try {
            Class aClass = Class.forName("com.fengzai.demo.annotation.MyController");
            Set<String> types = new HashSet<String>(){{{
                this.add(aClass.getTypeName());
            }}};
            new HashMap<String, Object>();
            obtainAnnotation(aClass, types);
            for (String a : types) {
                System.out.println("注解：" + a);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Set<String> obtainAnnotation(Class tClass, Set<String> typeNames) {
        if (tClass.isAnnotation()) {
            Annotation[] annotations = tClass.getAnnotations();
            if (ArrayUtil.isNotEmpty(annotations)) {
                for (Annotation a : annotations) {
                    String typeName = a.annotationType().getTypeName();
                    typeNames.add(typeName);
                    if (!typeNames.contains(typeName)) {
                        return obtainAnnotation(a.annotationType(), typeNames);
                    }
                }
            }
        }
        return typeNames;
    }
}
